$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/agentshopadmin/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'ID', width: 50, key: true },
			{ label: '管理员名称', name: 'adminName', index: 'ADMIN_NAME', width: 80 }, 			
			{ label: '管理员手机', name: 'adminPhone', index: 'ADMIN_PHONE', width: 80 }, 			
			// { label: '所属代理商', name: 'adminParent', index: 'ADMIN_PARENT', width: 80 },
            { label: '所属代理商', name: 'adminParent', index: 'ADMIN_PARENT', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var shopName ="";
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/agent/info/"+rowdata.adminParent,
                        success: function(r){
                            shopName = r.agent.shopName;

                        }
                    });
                    return shopName;
                }
            },
			// { label: '所属业务员', name: 'adminBusParent', index: 'ADMIN_BUS_PARENT', width: 80 },
            { label: '所属业务员', name: 'adminBusParent', index: 'ADMIN_BUS_PARENT', width: 80,formatter: function(cellvalue, options, rowdata) {
                    var shopName ="";
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/agentshopbus/infobus/"+rowdata.adminBusParent,
                        success: function(r){
                            if (null == r.agentShopBus) {
                                shopName="";
                            } else {
                                shopName = r.agentShopBus.busName;
                            }
                        }
                    });
                    return shopName;
                }
            },
			{ label: '佣金百分比', name: 'adminRatio', index: 'ADMIN_RATIO', width: 80 }, 			
			// { label: '可打开所属机柜', name: 'adminOpen', index: 'ADMIN_OPEN', width: 80 },
            { label: '可打开所属机柜', name: 'adminOpen', width: 80, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span class="label label-danger">是</span>' :
                        '<span class="label label-success">否</span>';
                }},
			// { label: '状态', name: 'status', index: 'STATUS', width: 80 },
            { label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span class="label label-danger">正常</span>' :
                        '<span class="label label-success">禁止</span>';
                }},
            { label: '操作', name: 'sy', width: 100,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' onclick='vm.update("+ rowdata.id +","+rowdata.statu+")'>" +
                        "修改</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' onclick='vm.del("+ rowdata.id +")'>" +
                        "删除</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    $.get(baseURL + "sys/agentshopbus/getBuss/", function(r){
        var html=' <select id="adminBusParents" class="form-control" style="font-size: 15px">'
        for(var i=0;i<r.list.length;i++){
            html+='<option value="'+r.list[i].busSn+'">'+r.list[i].busName+'</option>;'
        }
        html+='</select>'
        $("#adminBusParent").html(html);
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            adminName: null,
            adminParent:null,
            adminBusParent: null,
            adminPhone: null,
            status: null
        },
        qcx: {
            shopPhone: null,
            shopName: null,
            status: null
        },
        qxz: {
            shopPhone: null,
            shopName: null,
            status: null
        },
		showList: true,
		title: null,
        busList: {},
        user: {},
        agentshopbus: {},
		agentShopAdmin: {
            shopNameCx:null,
            userId: null,
            username: null,
            salesid:null,
            busname:null
        }
	},
    user: {},
    agentshopbus: {},
    agentShopAdmin: {
        userId: null,
        username: null,
        adminSn:null,
        salesid:null,
        busname:null
    },
	methods: {
		query: function () {
			vm.reload();
		},
        queryAdmin: function () {
            vm.reloadAdmin();
        },
        queryAdmin1: function () {
            vm.reloadAdmin1();
        },
		add: function(){
            vm.getSysUser()
            vm.getBusAgent();
			vm.showList = false;
			vm.title = "新增";
			vm.agentShopAdmin = {
                userId: null,
                username: null
            };
		},
		update: function (id) {
            vm.getSysUser()
            vm.getBusAgent();
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            // alert($("#name").text()+"11")
            // alert($("#salesid").text()+"22")
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.agentShopAdmin.id == null ? "sys/agentshopadmin/save" : "sys/agentshopadmin/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.agentShopAdmin),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (id) {
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/agentshopadmin/delete",
                        contentType: "application/json",
                        data: JSON.stringify(id),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        addAgent: function () {
            $("#jqGridAdd").jqGrid({
                url: baseURL + 'sys/agent/list',
                datatype: "json",
                colModel: [
                    // { label: 'id', name: 'id', index: 'ID', width: 50, key: true },
                    { label: '代理商名称', name: 'shopName', index: 'SHOP_NAME', width: 80 },
                    { label: '代理商手机', name: 'shopPhone', index: 'SHOP_PHONE', width: 80 },
                    // { label: '代理模式 0 购买设备 1 投放设备', name: 'shopType', index: 'SHOP_TYPE', width: 80 },
                    { label: '代理模式', name: 'shopType', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">购买设备</span>' :
                                '<span class="label label-success">投放设备</span>';
                        }},
                    { label: '代理商上级', name: 'shopParent', index: 'SHOP_PARENT', width: 80 },
                    { label: '佣金百分比', name: 'shopRatio', index: 'SHOP_RATIO', width: 80 },
                    // { label: '0 可打开所属机柜 1 不可打开', name: 'isopen', index: 'isOPEN', width: 80 },
                    { label: '可打开所属机柜', name: 'isopen', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">是</span>' :
                                '<span class="label label-success">否</span>';
                        }},
                    // { label: '所属系统', name: 'sysSystem', index: 'SYS_SYSTEM', width: 80 },
                    // { label: '状态 0 正常 1 禁用', name: 'status', index: 'STATUS', width: 80 },
                    { label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">正常</span>' :
                                '<span class="label label-success">异常</span>';
                        }},
                    // { label: '备注', name: 'remarks', index: 'REMARKS', width: 80 },
                    // { label: '创建时间', name: 'createdTime', index: 'CREATED_TIME', width: 80 },
                    // { label: '登录密码', name: 'loginPassword', index: 'LOGIN_PASSWORD', width: 80 }
                    { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                            return "<a style='cursor:pointer;' onclick='vm.shopChoose("+ rowdata.id +")'>" +
                                "<span class='btn btn-primary'>选择</span></a>&nbsp;&nbsp;&nbsp;&nbsp;" ;
                        }}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPager2",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridAdd").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
            // vm.queryGroup();
            $('#myModal').modal();
        },
        shopChoose:function (id) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/agent/info/"+id,
                success: function(r){
                    $("#agentParent").html("");
                    $("#agentParent").append(r.agent.shopName);
                    vm.agentShopAdmin.adminParent=r.agent.id;
                }
            });
            $('#myModal').modal("hide");

        },

        addAdmin: function () {
            $("#jqGridAdd1").jqGrid({
                url: baseURL + 'sys/agent/list',
                datatype: "json",
                colModel: [
                    // { label: 'id', name: 'id', index: 'ID', width: 50, key: true },
                    { label: '代理商名称', name: 'shopName', index: 'SHOP_NAME', width: 80 },
                    { label: '代理商手机', name: 'shopPhone', index: 'SHOP_PHONE', width: 80 },
                    // { label: '代理模式 0 购买设备 1 投放设备', name: 'shopType', index: 'SHOP_TYPE', width: 80 },
                    { label: '代理模式', name: 'shopType', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">购买设备</span>' :
                                '<span class="label label-success">投放设备</span>';
                        }},
                    { label: '代理商上级', name: 'shopParent', index: 'SHOP_PARENT', width: 80 },
                    { label: '佣金百分比', name: 'shopRatio', index: 'SHOP_RATIO', width: 80 },
                    // { label: '0 可打开所属机柜 1 不可打开', name: 'isopen', index: 'isOPEN', width: 80 },
                    { label: '可打开所属机柜', name: 'isopen', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">是</span>' :
                                '<span class="label label-success">否</span>';
                        }},
                    // { label: '所属系统', name: 'sysSystem', index: 'SYS_SYSTEM', width: 80 },
                    // { label: '状态 0 正常 1 禁用', name: 'status', index: 'STATUS', width: 80 },
                    { label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">正常</span>' :
                                '<span class="label label-success">异常</span>';
                        }},
                    // { label: '备注', name: 'remarks', index: 'REMARKS', width: 80 },
                    // { label: '创建时间', name: 'createdTime', index: 'CREATED_TIME', width: 80 },
                    // { label: '登录密码', name: 'loginPassword', index: 'LOGIN_PASSWORD', width: 80 }
                    { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                            return "<a style='cursor:pointer;' onclick='vm.shopChoose1("+ rowdata.id +")'>" +
                                "<span class='btn btn-primary'>选择</span></a>&nbsp;&nbsp;&nbsp;&nbsp;" ;
                        }}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPager1",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridAdd1").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
            // vm.queryGroup();
            $('#myModal1').modal();
        },
        shopChoose1:function (id) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/agent/info/"+id,
                success: function(r){
                    $("#agentParentQuery1").html("");
                    $("#agentParentQuery1").append(r.agent.shopName);
                    vm.agentShopAdmin.shopNameCx = r.agent.id;
                    // vm.agentShopAdmin.adminParent=r.agent.id;
                }
            });
            $('#myModal1').modal("hide");

        },
        getSysUser: function () {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/agentshopadmin/getSysUsers",
                        success: function (r) {
                            console.log(r);
                            if (r.code == 0) {
                                vm.user = r.data;
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                    console.log(vm);
                },
                setUsername: function (id) {
                    for (let i = 0; i < vm.user.length; i++) {
                        if (id == vm.user[i].userId) {
                            vm.agentShopAdmin.adminName = vm.user[i].username;
                        }
                    }
                    console.log(vm);
                },
        getBusAgent: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/agentshopbus/getUsers",
                success: function (r) {
                    console.log(r);
                    if (r.code == 0) {
                        vm.agentshopbus = r.data;
                    } else {
                        layer.alert(r.msg);
                    }
                }
            });
            console.log(vm);
        },
        setBusUsername: function (id) {
            for (let i = 0; i < vm.agentshopbus.length; i++) {
                if (id == vm.agentshopbus[i].id) {
                    vm.agentShopAdmin.busname = vm.agentshopbus[i].busName;
                }
            }
            console.log(vm);
        },
		getInfo: function(id){
			$.get(baseURL + "sys/agentshopadmin/info/"+id, function(r){
                //代理商
                $.ajax({
                    type: "POST",
                    async:false,
                    url: baseURL + "sys/agent/info/"+r.agentShopAdmin.adminParent,
                    success: function(r1){
                        $("#agentParent").text(r1.agent.shopName);
                    }
                });
                vm.agentShopAdmin = r.agentShopAdmin;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.q.adminParent = vm.agentShopAdmin.shopNameCx;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {'status': vm.q.status,
                    'adminName': vm.q.adminName,
                    'adminParent': vm.q.adminParent,
                    'adminBusParent': vm.q.adminBusParent,
                    'adminPhone': vm.q.adminPhone
                },
                page:page
            }).trigger("reloadGrid");
		},
        reloadAdmin: function (event) {
            vm.showList = true;
            var page = $("#jqGridAdd").jqGrid('getGridParam','page');
            $("#jqGridAdd").jqGrid('setGridParam',{
                postData: {'status': vm.qxz.status,
                    'shopName': vm.qxz.shopName,
                    'shopPhone': vm.qxz.shopPhone
                },
                page:page
            }).trigger("reloadGrid");
        },
        reloadAdmin1: function (event) {
            var page = $("#jqGridAdd1").jqGrid('getGridParam','page');
            $("#jqGridAdd1").jqGrid('setGridParam',{
                postData: {'status': vm.qcx.status,
                    'shopName': vm.qcx.shopName,
                    'shopPhone': vm.qcx.shopPhone
                },
                page:page
            }).trigger("reloadGrid");
        },
	}
});